package com.kyzh.core.dialog

import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AlertDialog

private var addtodoDialog: AlertDialog?=null
fun Context.showDynamicDiscussAddDialog(listener:()->Unit){
    addtodoDialog = AlertDialog.Builder(this).setPositiveButton("确定"){
            _, _ ->
        addtodoDialog?.dismiss()
        listener()
    }.setNegativeButton("取消") {
            _, _ ->
        addtodoDialog?.dismiss()
    }.create()
        addtodoDialog?.apply {
            setTitle("温馨提示")
            setMessage("是否删除此评论")
        }

    addtodoDialog?.show()

    }




private fun getGlobalLayoutListener(decorView: View, contentView: View): ViewTreeObserver.OnGlobalLayoutListener? {
    return ViewTreeObserver.OnGlobalLayoutListener {
        val r = Rect()
        decorView.getWindowVisibleDisplayFrame(r)
        val height = decorView.context.resources.displayMetrics.heightPixels
        val diff = height - r.bottom
        if (diff != 0) {
            if (contentView.paddingBottom !== diff) {
                contentView.setPadding(0, 0, 0, diff)
            }
        } else {
            if (contentView.paddingBottom !== 0) {
                contentView.setPadding(0, 0, 0, 0)
            }
        }
    }
}
fun Context.showKeyboard() {
    (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).toggleSoftInput(
        0,
        InputMethodManager.HIDE_NOT_ALWAYS
    )
}

fun View.hideKeyboard(context: Activity) {
    (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
        windowToken,
        0
    )
}

 fun View.checkInputMethodVisible(): Boolean {
    val imm = context
        .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    return imm.hideSoftInputFromWindow(this.windowToken, 0)
}